package com.idak.rdv.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.idak.rdv.dao.UserDao;
import com.idak.rdv.db.User;

@Repository("userDao")
public class UserDaoImpl extends RdvHibernateDao<User, Integer> implements UserDao {

	public boolean authenticateUser(String login, String password) {
		boolean isUserValid = false;
		if(login != null && password != null){
			Session session = getCurrentSession();
			Criteria criteria = session.createCriteria(getEntityClass());
			criteria.add(Restrictions.eq("login", login))
					.add(Restrictions.eq("password", password));
			User user =  (User) criteria.uniqueResult();
			if(user != null){
				isUserValid = true;
			}
			
		}
		return isUserValid;
	}

	public User findUserByLogin(String login) {
		Session session = getCurrentSession();
		Criteria criteria = session.createCriteria(getEntityClass());
		criteria.add(Restrictions.eq("login", login));
		return (User) criteria.uniqueResult();
	}

	public boolean checkLogin(String login) {
		boolean exist = false;
		Session session = getCurrentSession();
		Criteria criteria = session.createCriteria(getEntityClass());
		criteria.add(Restrictions.eq("login", login));
		User user = (User) criteria.uniqueResult();
		if(user != null){
			exist=true;
		}
		return exist;
	}
	
	@Override
	public Class<User> getEntityClass() {
		return User.class;
	}
}
